<?php


namespace app\admin\model;


use think\Cache;
use think\Model;

class SmsConfig extends Model
{

    //自动写入时间
    protected $autoWriteTimestamp = true;

    /**
     * 时间格式化
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i:s';

    public static $getSmsConfigKey = 'getSmsConfig:';
    public static $getSmsIdByTypeKey = 'getSmsIdByType:';

    //获取单条短信配置
    public static function getSmsConfig($id)
    {
        $info = Cache::store('redis')->get(self::$getSmsConfigKey . $id);
        $info = [];
        if (empty($info)) {
            $info = self::get($id);
            Cache::store('redis')->set(self::$getSmsConfigKey . $id, $info, 86400 * 7);
        }
        return $info;
    }

    //更新单挑短信配置
    public static function saveSmsConfigCache($id)
    {
        $info = self::get($id);
        Cache::store('redis')->set(self::$getSmsConfigKey . $id, $info, 86400 * 7);
        return $info;
    }

    //获取短信通道
    public static function getSmsIdByType($type)
    {
        $info = Cache::store('redis')->get(self::$getSmsIdByTypeKey . $type);
        $info = [];
        if (empty($info)) {
            $info = self::where(['type' => $type, 'switch' => 1])->find();
            Cache::store('redis')->set(self::$getSmsIdByTypeKey . $type, $info, 86400 * 7);
        }
        return $info;
    }

    //获取短信通道
    public static function saveSmsIdByTypeCache($type)
    {
        $info = self::where(['type' => $type, 'switch' => 1])->find();
        Cache::store('redis')->set(self::$getSmsIdByTypeKey . $type, $info, 86400 * 7);
        return $info;
    }
}